#include "GameScene.h"
#include "AnimationUnit.h"

using namespace cocos2d;

static int g_iXSpeed = 100;
static int g_iYSpeed = 100;

enum
{
	STATE_STOP = 0,
	STATE_UP,
	STATE_LEFT,
	STATE_DOWN,
	STATE_RIGHT
};
static int g_iDirection = 0;	//up=1, left=2, down=3, right=4, stop=0

GameScene::GameScene(void)
	:m_pPlane(NULL)
{
}


GameScene::~GameScene(void)
{
	CCDirector::sharedDirector()->getOpenGLView()->SetWin32KeyLayer(NULL);
	for(ResourceIterator it = m_pResource.begin(); it != m_pResource.end(); it++)
		delete *it;
	m_pResource.clear();
}

bool GameScene::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		CResourceUnit* ru = new CResourceUnit;
		CC_BREAK_IF(!ru || !ru->Init("1"));
		m_pResource.push_back(ru);
		ru->AddToLayer(this,0);

// 		CCSpriteBatchNode* sbn = CCSpriteBatchNode::create("1.pvr");
// 		CC_BREAK_IF(!sbn);
// 		this->addChild(sbn);
// 
// 		CCSpriteFrameCache* sfc = CCSpriteFrameCache::sharedSpriteFrameCache();
// 		CC_BREAK_IF(!sfc);
// 		sfc->addSpriteFramesWithFile("1.plist");

		m_pPlane = CCSprite::createWithSpriteFrameName("plane.png");
		//m_pPlane = CCSprite::create("plane.png");
		CC_BREAK_IF(!m_pPlane);
		
		m_pPlane->setPosition(ccp(size.width/2, size.height/2));
		//this->addChild(m_pPlane,0);
		ru->AddSprite(m_pPlane);
		//sbn->addChild(m_pPlane,0);

		CCDirector::sharedDirector()->getOpenGLView()->SetWin32KeyLayer(this);

		this->schedule(schedule_selector(GameScene::flying));

		CAnimationUnit *au = new CAnimationUnit;
		CC_BREAK_IF(!au->Init(ru,0,0,1));
		au->SetDelayPerUnit(0.5f);
		au->SetRestoreOriginalFrame(true);
		au->SetLoops(-1);
		au->Play();
// 		CCAnimation* anim = CCAnimation::create();
// 		CC_BREAK_IF(!anim);
// 
// 		anim->setDelayPerUnit(0.2f);
// 		anim->setRestoreOriginalFrame(true);
// 		anim->setLoops(-1);
// 
// 		CCSprite* sp = CCSprite::createWithSpriteFrameName("horse.png");
// 		CC_BREAK_IF(!sp);
// 		sp->setPosition(ccp(100,100));
// 		sp->setAnchorPoint(ccp(0,0));
// 		//this->addChild(sp,0);
// 		sbn->addChild(sp,0);
// 
// 		CCTexture2D* tex = sp->getTexture();
// 		CCRect rt = sp->getTextureRect();
// 		int x = rt.origin.x;
// 		int y = rt.origin.y;
// 		int w = rt.size.width/5;
// 		int h = rt.size.height;
// 		for(int i = 0; i < 5; i++)
// 		{
// 			anim->addSpriteFrameWithTexture(tex,CCRectMake(i*w+x,y+i*10,w,h-i*10));
// 		}
// 
// 		CCFiniteTimeAction* animate = CCAnimate::create(anim);
// 		CC_BREAK_IF(!animate);
// 
// 		sp->runAction(animate);

		bRet = true;
	} while (0);

	return bRet;
}

cocos2d::CCScene* GameScene::scene()
{
	CCScene* scene = NULL;

	do 
	{
		scene = CCScene::create();
		CC_BREAK_IF(!scene);

		GameScene* layer = GameScene::create();
		CC_BREAK_IF(!layer);

		scene->addChild(layer);
	} while (0);

	return scene;
}

void GameScene::processWin32KeyPress( UINT message, WPARAM wParam, LPARAM lParam )
{
	switch(message)
	{
	case WM_KEYDOWN:
		doKeyDown(wParam,lParam);
		break;
	case WM_KEYUP:
		doKeyUp(wParam,lParam);
		break;
	default:
		break;
	}
}

void GameScene::doKeyDown(WPARAM wParam, LPARAM lParam)
{
	switch(wParam)
	{
	case VK_DOWN:
		g_iDirection = STATE_DOWN;
		break;
	case VK_UP:
		g_iDirection = STATE_UP;
		break;
	case VK_LEFT:
		g_iDirection = STATE_LEFT;
		break;
	case VK_RIGHT:
		g_iDirection = STATE_RIGHT;
		break;
	default:
		break;
	}
}

void GameScene::doKeyUp(WPARAM wParam, LPARAM lParam)
{
	switch(wParam)
	{
	case VK_DOWN:
	case VK_UP:
	case VK_LEFT:
	case VK_RIGHT:
		g_iDirection = STATE_STOP;
		break;
	default:
		break;
	}
}

void GameScene::flying( float dt )
{
	CCPoint position=ccp(m_pPlane->getPosition().x,m_pPlane->getPosition().y);

	switch (g_iDirection)
	{
	case STATE_UP:
		position.y += g_iYSpeed*dt;
		break;
	case STATE_LEFT:
		position.x -= g_iXSpeed*dt;
		break;
	case STATE_DOWN:
		position.y -= g_iYSpeed*dt;
		break;
	case STATE_RIGHT:
		position.x += g_iXSpeed*dt;
		break;
	default:
		break;
	}

	CCSize size=CCDirector::sharedDirector()->getWinSize();
	CCRect rect=CCRectMake(0,0,size.width,size.height);

	if(rect.containsPoint(position))
	{
		m_pPlane->setPosition(position);
	}
}
